//
//  TabsView.swift
//  MyTest
//
//  Created by guanzhongwang on 2021/5/8.
//

import UIKit

class TabsView: UIView {
    private let scroll = UIScrollView()
    private let tip = UIView()
    private var lastbtn:UIButton?
    
    private var buttons:Array<UIButton> = []

    var valueChanged:(((String,Int))->Void)?
    private var index = 0
    func nextTab(_ index:Int)  {
        self.updateFrame(buttons[index])
    }
    init(items:Array<TabsModel>,index:Int = 0) {
        super.init(frame: CGRect.zero)
        self.index = index
        backgroundColor = .white
        scroll.showsHorizontalScrollIndicator = false
        scroll.bounces = false
        addSubview(scroll)
        scroll.snp.makeConstraints({
            $0.left.right.equalTo(0)
            $0.height.equalToSuperview()
        })
        tip.layer.cornerRadius = 1
        tip.clipsToBounds = true
        addSubview(tip)
        tip.backgroundColor = RED_COLOR
        createMenus(items: items)
    }
    func createMenus(items:Array<TabsModel>) {
        var temp:UIButton?
        for i in 0..<items.count {
            let item = items[i]
            let btn = UIButton(type: .custom)
            btn.setTitleColor(B000_COLOR, for: .selected)
            btn.setTitleColor(B666_TITLE_COLOR, for: .normal)
            btn.addTarget(self, action: #selector(btnclick), for: .touchUpInside)
            btn.contentEdgeInsets = UIEdgeInsets.init(top: 0, left: 15, bottom: 0, right: 15)
            btn.titleLabel?.font = Font_Size_15
            btn.setTitle(item.name, for: .normal)
            scroll.addSubview(btn)
            btn.snp.makeConstraints({
                if temp == nil{
                    $0.left.equalTo(0)
                }else{
                    $0.left.equalTo(temp!.snp.right)
                }
                $0.top.equalTo(0)
                if i==items.count-1{
                    $0.right.equalToSuperview()
                }
                $0.height.equalToSuperview()
            })
            temp = btn
            if i == index {
                btn.isSelected = true
                tip.snp.makeConstraints({
                    $0.width.equalTo(btn.snp.width).dividedBy(3)
                    $0.bottom.equalToSuperview().inset(2)
                    $0.height.equalTo(2)
                    $0.centerX.equalTo(btn)
                })
            }
            buttons.append(btn)
        }
        lastbtn = buttons[index]

    }
    @objc func btnclick(btn:UIButton){
        self.updateFrame(btn)
        self.valueChanged!((btn.currentTitle!,btn.tag))
    }
    private func updateFrame(_ btn:UIButton) {
        lastbtn?.isSelected = false
        lastbtn?.titleLabel?.font = Font_Size_15
        
        btn.isSelected = true
        btn.titleLabel?.font = Font_Size_15
        
        tip.snp.remakeConstraints({
            $0.centerX.equalTo(btn)
            $0.width.equalTo(btn.snp.width).dividedBy(2)
            $0.height.equalTo(2)
            $0.bottom.equalToSuperview().inset(2)
        })
        UIView.animate(withDuration: 0.5) {
            [weak self] in
            self?.layoutIfNeeded()
        }
        let bx = btn.frame.origin.x
        let bw = btn.frame.size.width
        let w = scroll.frame.width

        if bx + bw/2 > w/2 && bx + bw/2 - w/2 < scroll.contentSize.width - w {
            scroll.setContentOffset(CGPoint.init(x: bx + bw/2 - w/2, y: 0), animated: true)
        }else{
            if(bx + bw/2 - w/2 < scroll.contentSize.width - w){
                scroll.setContentOffset(CGPoint.zero, animated: true)
            }else{
                scroll.setContentOffset(CGPoint.init(x: scroll.contentSize.width-w, y: 0), animated: true)
            }
        }
        lastbtn = btn
    }
    override func draw(_ rect: CGRect) {
        super.draw(rect)
        addBottomBorder()
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
